<?php
/*
 * @Author: 哲学家乔布斯 <wuyangii@qq.com>
 * @Date: 2022-10-20 21:41:37
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2022-11-07 22:37:15
 * @Descripttion: 首页
 */

namespace app\api\controller\user\v1_0;

class Index extends Common
{
    protected $noNeedRight = ['test'];
    /**
     * @Author: 哲学家乔布斯 <wuyangii@qq.com>
     * @brief: 
     * @return 
     * @Descripttion: 首页默认接口
     */    
    public function index(){
        $param = request()->param();
        #轮播图
        $banner_list = (new \app\common\model\Adv())
                        ->field(['id','title','image','goods_id'])
                        ->append(['image_text'])->hidden(['image'])
                        ->where(['agent_id'=>$this->agent_id,'type' => 0])
                        ->order('sort asc')
                        ->select();
        #图标分类
        $img_cate_list = (new \app\common\model\GoodsCate())
                        ->field(['id','img','title'])
                        ->where(['is_img_index' => 1,'is_display' => 1])
                        ->order('sort desc')
                        ->select();
        #文字分类
        $txt_cate_list = (new \app\common\model\GoodsCate())
                    ->field(['id','title'])
                    ->where(['is_txt_index' => 1,'is_display' => 1])
                    ->order('sort desc')
                    ->select();
        #商品信息
        $where = [];
        if(!empty($param['txt_cate_id'])){
            $where['goods.cate_id'] = $param['txt_cate_id'];
        }
        $page = empty(request()->param('page')) ? 1 : request()->param('page');
        $limit = empty(request()->param('limit')) ? 10 : request()->param('limit');
        $goods_list = (new \app\common\model\AgentGoodsSku())
                    ->alias("agent_goods_sku")
                    ->join("{$this->tdb}goods goods","goods.id = agent_goods_sku.goods_id")
                    ->field([
                        'agent_goods_sku.id',
                        'goods.img',
                        'goods.title',
                        'goods.introduce',
                        'agent_goods_sku.stock',
                        'agent_goods_sku.end_time',
                        'agent_goods_sku.money',
                        'agent_goods_sku.money_primary',
                        'agent_goods_sku.sales',
                        'agent_goods_sku.team_rebate',
                        ])
                    ->append(['img','end_time_text'])
                    ->page($page)
                    ->limit($limit)
                    ->where($where)
                    ->where([
                        'agent_goods_sku.is_display' => 1,
                        'goods.is_display' => 1,
                    ])
                    ->where(['agent_goods_sku.agent_id' => $this->agent_id])
                    ->order('agent_goods_sku.sort desc,agent_goods_sku.id desc ')
                    ->select();
            $recommend_list = (new \app\common\model\AgentGoodsSku())
                    ->alias("agent_goods_sku")
                    ->join("{$this->tdb}goods goods","goods.id = agent_goods_sku.goods_id")
                    ->field([
                        'agent_goods_sku.id',
                        'goods.img',
                        'goods.title',
                        'goods.introduce',
                        'agent_goods_sku.stock',
                        'agent_goods_sku.end_time',
                        'agent_goods_sku.money',
                        'agent_goods_sku.money_primary',
                        'agent_goods_sku.sales'
                        ])
                    ->append(['img','end_time_text'])
                    ->page($page)
                    ->limit($limit)
                    ->where([
                        'agent_goods_sku.is_display' => 1,
                        'goods.is_display' => 1,
                        'agent_goods_sku.is_recommend' => 1,
                    ])
                    ->where(['agent_goods_sku.agent_id' => $this->agent_id])
                    ->order('agent_goods_sku.sort desc,agent_goods_sku.id desc ')
                    ->select();
        #默认提货点
        $longitude = !empty(request()->param('longitude')) ? request()->param('longitude') : '125.3245'; // 用户经度
        $latitude = !empty(request()->param('latitude')) ? request()->param('latitude') : '43.874762';//用户纬度
        if(empty($this->bus_id)){
            $bus =(new \app\common\model\Bus())
            ->whereNotNull('longitude')
            ->whereNotNull('latitude')
            ->field("ROUND(
                6378.138 * 2 * ASIN(
                    SQRT(
                    POW( SIN(( $latitude * PI()/ 180-latitude * PI()/ 180 )/ 2 ), 2 )+ COS( $latitude * PI()/ 180 )* COS( latitude * PI()/ 180 )* POW( SIN(( $longitude * PI()/ 180-longitude * PI()/ 180 )/ 2 ), 2 )))* 1000 
            ) AS distancea,nickname,id as bus_id,id as city_id,avatar")
            ->order('distancea asc')
            ->find();
        }else{
            $bus =(new \app\common\model\Bus())
            ->whereNotNull('longitude')
            ->whereNotNull('latitude')
            ->field("ROUND(
                6378.138 * 2 * ASIN(
                    SQRT(
                    POW( SIN(( $latitude * PI()/ 180-latitude * PI()/ 180 )/ 2 ), 2 )+ COS( $latitude * PI()/ 180 )* COS( latitude * PI()/ 180 )* POW( SIN(( $longitude * PI()/ 180-longitude * PI()/ 180 )/ 2 ), 2 )))* 1000 
            ) AS distancea,nickname,id as bus_id,id as city_id,avatar")
            ->order('distancea asc')
            ->where(['id' => $this->bus_id])
            ->find();
        }
       
        return self::successResult("成功",[
            'adv' => $banner_list,
            'img_cate_list' => $img_cate_list,
            'txt_cate_list' => $txt_cate_list,
            'goods' => $goods_list,
            'bus' => $bus,
            'config' => aconfig("title",$this->agent_id),
            'datasa' => $recommend_list,
        ]);
    }

    public function bus_list(){
        $longitude = !empty(request()->param('longitude')) ? request()->param('longitude') : '125.3245'; // 用户经度
        $latitude = !empty(request()->param('latitude')) ? request()->param('latitude') : '43.874762';//用户纬度
        $data =(new \app\common\model\Bus())
        ->whereNotNull('longitude')
        ->whereNotNull('latitude')
        ->field("ROUND(
            6378.138 * 2 * ASIN(
                SQRT(
                POW( SIN(( $latitude * PI()/ 180-latitude * PI()/ 180 )/ 2 ), 2 )+ COS( $latitude * PI()/ 180 )* COS( latitude * PI()/ 180 )* POW( SIN(( $longitude * PI()/ 180-longitude * PI()/ 180 )/ 2 ), 2 )))* 1000 
        ) AS distancea,nickname,id as bus_id,avatar,address,phone")
        ->order('distancea asc')
        // ->having('distancea < 800')
        ->append(['phone'])
        ->withAttr('phone',function($value,$data){
            return substr_replace($value,'****',3,4);
        })->select();
        return self::successResult("",$data);
    }
    public function recommend(){
        $bus_id = $this->bus_id;
        $agent_id = $this->agent_id;
        $page = empty(request()->param('page')) ? 1 : request()->param('page');
        $limit = empty(request()->param('limit')) ? 10 : request()->param('limit');
        $user = empty($this->UserInfo) ? '' : (new \app\common\model\User())->where(['id' => $this->UserInfo['id']])->find();
        $goods_list = (new \app\common\model\AgentGoodsSku())
                    ->alias("agent_goods_sku")
                    ->join("{$this->tdb}goods goods","goods.id = agent_goods_sku.goods_id")
                    ->field([
                        'agent_goods_sku.id',
                        'goods.img',
                        'goods.title',
                        'goods.introduce',
                        'agent_goods_sku.money',
                        'agent_goods_sku.money_primary', 
                        'agent_goods_sku.team_rebate',
                        'agent_goods_sku.stock'
                        ])
                    ->where(['agent_goods_sku.agent_id' => $this->agent_id,'agent_goods_sku.is_recommend' => 1])
                    ->where([
                        'agent_goods_sku.is_display' => 1,
                        'goods.is_display' => 1,
                        'agent_goods_sku.is_del' => 0,
                        'goods.is_del' => 0,
                    ])
                    ->append(['img','cart_num'])
                    ->withAttr("cart_num",function($value,$data) use($user,$agent_id,$bus_id){
                        return empty($user)? 0 : (new \app\common\model\AgentGoodsCart())->where([
                            'goods_sku_id' => $data['id'],
                            'user_id' => $user['id'],
                            'agent_id' => $agent_id,
                            'bus_id' => $bus_id,
                            ])->sum('num');
                    })
                    ->page($page)
                    ->limit($limit)
                    ->order('agent_goods_sku.sort desc ,agent_goods_sku.id desc')
                    ->select();
        return self::successResult("成功",$goods_list);
    }

}
